<template>
  <div>
    <el-card class="box-card">
      <!-- 搜索区域 -->
      <div class="search-box">
        <el-input
          v-model="searchEmpNum"
          placeholder="请输入员工编号"
          style="width: 200px"
          class="filter-item"
        ></el-input>
        <el-button type="primary" @click="searchByEmpNum">查询</el-button>
        <el-button type="success" @click="handleAdd">添加调动记录</el-button>
      </div>

      <!-- 表格区域 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="序号" width="80"></el-table-column>
        <el-table-column prop="empNum" label="员工编号" width="100"></el-table-column>
        <el-table-column prop="oldDepartment" label="原部门" width="120"></el-table-column>
        <el-table-column prop="oldPosition" label="原职位" width="120"></el-table-column>
        <el-table-column prop="newDepartment" label="新部门" width="120"></el-table-column>
        <el-table-column prop="newPosition" label="新职位" width="120"></el-table-column>
        <el-table-column prop="transferDate" label="调动日期" width="180"></el-table-column>
        <el-table-column prop="transferReason" label="调动原因"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="员工编号" prop="empNum" v-if="!isEdit">
          <el-input v-model="form.empNum"></el-input>
        </el-form-item>
        <el-form-item label="原部门" prop="oldDepartment">
          <el-input v-model="form.oldDepartment"></el-input>
        </el-form-item>
        <el-form-item label="原职位" prop="oldPosition">
          <el-input v-model="form.oldPosition"></el-input>
        </el-form-item>
        <el-form-item label="新部门" prop="newDepartment">
          <el-input v-model="form.newDepartment"></el-input>
        </el-form-item>
        <el-form-item label="新职位" prop="newPosition">
          <el-input v-model="form.newPosition"></el-input>
        </el-form-item>
        <el-form-item label="调动日期" prop="transferDate">
          <el-date-picker
            v-model="form.transferDate"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="调动原因" prop="transferReason">
          <el-input type="textarea" v-model="form.transferReason"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      searchEmpNum: "",
      dialogVisible: false,
      dialogTitle: "添加调动记录",
      isEdit: false,
      form: {
        empNum: "",
        oldDepartment: "",
        oldPosition: "",
        newDepartment: "",
        newPosition: "",
        transferDate: "",
        transferReason: ""
      },
      rules: {
        empNum: [{ required: true, message: "请输入员工编号", trigger: "blur" }],
        oldDepartment: [{ required: true, message: "请输入原部门", trigger: "blur" }],
        oldPosition: [{ required: true, message: "请输入原职位", trigger: "blur" }],
        newDepartment: [{ required: true, message: "请输入新部门", trigger: "blur" }],
        newPosition: [{ required: true, message: "请输入新职位", trigger: "blur" }],
        transferDate: [{ required: true, message: "请选择调动日期", trigger: "change" }],
        transferReason: [{ required: true, message: "请输入调动原因", trigger: "blur" }]
      },
      currentTransferId: null
    };
  },
  created() {
    this.getAllRecords();
  },
  methods: {
    // 获取所有调动记录
    getAllRecords() {
      this.$http.get("/api/employee_transfers")
        .then(response => {
          if (response.data.code === 200) {
            this.tableData = response.data.data;
          } else {
            this.$message.error(response.data.msg);
          }
        });
    },

    // 按员工编号搜索
    searchByEmpNum() {
      if (!this.searchEmpNum) {
        this.getAllRecords();
        return;
      }
      this.$http.get(`/api/employee_transfers/${this.searchEmpNum}`)
        .then(response => {
          if (response.data.code === 200) {
            this.tableData = response.data.data;
          } else {
            this.$message.error(response.data.msg);
          }
        });
    },

    // 打开添加对话框
    handleAdd() {
      this.isEdit = false;
      this.dialogTitle = "添加调动记录";
      this.form = {
        empNum: "",
        oldDepartment: "",
        oldPosition: "",
        newDepartment: "",
        newPosition: "",
        transferDate: "",
        transferReason: ""
      };
      this.dialogVisible = true;
    },

    // 打开编辑对话框
    handleEdit(row) {
      this.isEdit = true;
      this.dialogTitle = "编辑调动记录";
      this.currentTransferId = row.transferId;
      this.form = { ...row };
      this.dialogVisible = true;
    },

    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            // 更新记录
            this.$http.put(`/api/employee_transfers/${this.currentTransferId}`, JSON.stringify(this.form), {
              headers: {
                'Content-Type': 'application/json'
              }
            }).then(response => {
                if (response.data.code === 200) {
                  this.$message.success("更新成功");
                  this.dialogVisible = false;
                  this.getAllRecords();
                } else {
                  this.$message.error(response.data.msg);
                }
              });
          } else {
            // 添加记录
            this.$http.post("/api/employee_transfers", JSON.stringify(this.form), {
              headers: {
                'Content-Type': 'application/json'
              }
            }).then(response => {
                if (response.data.code === 200) {
                  this.$message.success("添加成功");
                  this.dialogVisible = false;
                  this.getAllRecords();
                } else {
                  this.$message.error(response.data.msg);
                }
              });
          }
        }
      });
    },

    // 删除记录
    handleDelete(row) {
      this.$confirm('确认删除该调动记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete(`/api/employee_transfers/${row.transferId}`)
          .then(response => {
            if (response.data.code === 200) {
              this.$message.success("删除成功");
              this.getAllRecords();
            } else {
              this.$message.error(response.data.msg);
            }
          });
      }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.search-box {
  margin-bottom: 20px;
}
.filter-item {
  margin-right: 10px;
}
</style>
